CFLAGS = -std=gnu99 -Wall -fpic -pg -ftest-coverage -fprofile-arcs -lm

all: playdom player testdominion

rngs.o: rngs.h rngs.c
	gcc -c rngs.c -g  $(CFLAGS)

dominion.o: dominion.h dominion.c rngs.o
	gcc -c dominion.c -g  $(CFLAGS)

playdom: dominion.o playdom.c
	gcc -o playdom playdom.c -g dominion.o rngs.o $(CFLAGS)

testdominion: testdominion.c dominion.o rngs.o interface.o
	gcc -o testdominion -g testdominion.c dominion.o rngs.o interface.o $(CFLAGS)

interface.o: interface.h interface.c
	gcc -c interface.c -g $(CFLAGS)

testutils.o: testutils.h testutils.c
	gcc -c testutils.c -g $(CFLAGS)

player: player.c interface.o
	gcc -o player player.c -g  dominion.o rngs.o interface.o $(CFLAGS)

UNITTESTS =	unittest1 unittest2 unittest3 \
			cardtest1 cardtest2 cardtest3 cardtest4 \
			testremodel testmine testfeast testsalvager testseahag \
			testminion testadventurer testbaron
INTEGRATIONTESTS =	teststackingcoinbonus testcombos
TEST_DEPS = dominion.o rngs.o interface.o testutils.o 
TEST_COMPILE = gcc -o $@ $< -g $(TEST_DEPS) $(CFLAGS)

$(UNITTESTS): %: %.c $(TEST_DEPS)
	$(TEST_COMPILE)
$(INTEGRATIONTESTS): %: %.c $(TEST_DEPS)
	$(TEST_COMPILE)

unittestresults.out: $(UNITTESTS)
	rm -f *.gcda *.gcov gmon.out
	rm -f unittestresults.out
	touch unittestresults.out
	for unittest in $(UNITTESTS) ; do \
		./$$unittest | tee -a unittestresults.out; \
	done
	echo "gcov output after all tests were run:" >> unittestresults.out
	gcov dominion.c >> unittestresults.out
	cat dominion.c.gcov >> unittestresults.out
	rm -f *.gcov gmon.out
.PHONY: unittestresults.out

integrationtestresults.out: $(INTEGRATIONTESTS)
	rm -f *.gcda *.gcov gmon.out
	rm -f integrationtestresults.out
	touch integrationtestresults.out
	for inttest in $(INTEGRATIONTESTS) ; do \
		./$$inttest | tee -a integrationtestresults.out; \
	done
	echo "gcov output after all tests were run:" >> integrationtestresults.out
	gcov dominion.c >> integrationtestresults.out
	cat dominion.c.gcov >> integrationtestresults.out
	rm -f *.gcov gmon.out
.PHONY: integrationtestresults.out

testdominion.out: testdominion
	rm -f *.gcda *.gcov gmon.out
	rm -f testdominion.out
	touch testdominion.out
	(./testdominion || echo "Run 1 Crashed!") | tee -a testdominion.out
	(./testdominion || echo "Run 2 Crashed!") | tee -a testdominion.out
	(./testdominion || echo "Run 3 Crashed!") | tee -a testdominion.out
	(./testdominion || echo "Run 4 Crashed!") | tee -a testdominion.out
	(./testdominion || echo "Run 5 Crashed!") | tee -a testdominion.out
	(./testdominion || echo "Run 6 Crashed!") | tee -a testdominion.out
	echo "gcov output after all tests were run:" >> testdominion.out
	gcov dominion.c >> testdominion.out
	cat dominion.c.gcov >> testdominion.out
	rm -f *.gcov gmon.out
.PHONY: testdominion.out

clean:
	rm -f *.o playdom player $(UNITTESTS) $(INTEGRATIONTESTS) testdominion *.so *.gcov *.gcno *.gcda *.out
.PHONY: clean
